package fr.ul.ade.prime.model;

import fr.ul.ade.prime.service.AdeWebService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * @author Schnell Michaël
 * @version 1.0
 */
public class AdeWebBroadcastReceiver extends BroadcastReceiver implements
		IntentConstants {

	@SuppressWarnings("unused")
	private static final String TAG = AdeWebBroadcastReceiver.class
			.getSimpleName();

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		//		Log.d(TAG, String.format("[onReceive] action: '%s'", //$NON-NLS-1$
		// action)) ;
		Intent serviceIntent = null;
		if (action.equals(SCHEDULE_STATE_UPDATE_ACTION)
				|| action.equals(SCHEDULE_AUTO_UPDATE_ACTION)) {
			serviceIntent = new Intent(context, AdeWebService.class);
			serviceIntent.setAction(action);
		} else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			boolean networkAvailable = !intent
					.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
							false);
			if (networkAvailable) {
				ConnectivityManager manager = (ConnectivityManager) context
						.getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo networkInfo = manager.getActiveNetworkInfo();
				networkAvailable = (networkInfo != null)
						&& networkInfo.isConnected();
			}// if
			AdeWebOptions.setNetworkAvailability(context, networkAvailable);
			serviceIntent = new Intent(context, AdeWebService.class);
			serviceIntent.setAction(action);
		}// if
		if (serviceIntent != null) {
			context.startService(serviceIntent);
		}// if
	}// onReceive(Context, Intent)

}// class AdeWebBroadcaseReceiver